<!--
	时间：2018-09-12
	描述：图片验证码
-->
<template>
	<view class="imgCodeMode" v-loading="showCode">
		<image :src="codeUrl" @click="getImgCode" class="img" :style="{width: width,height: height}"/>
	</view>
</template>

<script>
	import {
		base64ToPath
	} from '@/js_sdk/mmmm-image-tools/index.js'
	export default {
		props: {
			sign: String,
      width: String,
      height: String,
		},

		data() {
			return {
				codeUrl: '',
				showCode: true,
			}
		},

		mounted() {
			this.UPLOADAPI = this.$reqConfig.UPLOADAPI
			this.getImgCode();
		},

		methods: {
			getImgCode() {
				this.showCode = true;

				uni.request({
					url: this.UPLOADAPI + "public/verifyCode",
					responseType: 'arraybuffer',
					success: (res) => {
						let base64 = wx.arrayBufferToBase64(res.data);
						// console.log("base", base64)
						base64 = 'data:image/jpeg;base64,' + base64					
						this.$emit('update:sign', res.header.sign)					
						this.codeUrl=base64
						this.showCode = false;
					},
					fail(err) {
						console.log(err);
						this.showCode = false;
					}
				})

				/* this.$axios.get('public/verifyCode?'+Math.random(),{responseType: 'arraybuffer'}).then( (res)=>{
				  	// 后端需要的响应头中的信息参数赋值
					let codeImg =  'data:image/png;base64,' + btoa(
						new Uint8Array(res.data).reduce((data, byte) => data + String.fromCharCode(byte), '')
					)
					this.codeUrl = codeImg
					this.$emit('update:sign', res.headers.sign)
					this.showCode = false;
				}).catch((error) => {
				  console.log(error);
					this.showCode = false;
				}) */
			}
		}

	}
</script>

<style scoped>
	.imgCodeMode {
		height: 38px;
		padding: 0px !important;
		box-sizing: border-box;
		position: relative;
	}

	.img {
		width: 200rpx;
		height: 80rpx;
	}

	.el-loading-spinner .circular {
		margin: auto;
	}
</style>
